<?php


namespace App\Jobs\QiPao\Gift;


use App\Models\MainDB\Activity\ActivityNewYear;
use App\Models\MainDB\Activity\ActivityNewYearUser;
use Illuminate\Support\Facades\DB;
use function Matrix\determinant;

class ActivityJob
{
    protected $data;    //通过give_gift表的id取出来的送礼记录
    protected $failId = [];
    const jobName = 'ActivityJob';

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws \Matrix\Exception
     * @throws \Exception
     */
    public function handle()
    {
//        进来消费：{"id":623504,"user_id":3500,"gift_id":879,"number":1,"gift_user":3427,
//        "room_id":8,"pit_number":3,"add_time":1703687646,"total_price":"100.00","type":0,"coin":"0.00"
        $data = $this->data;
        $time = date("Y-m-d H:i:s");
        $activity_info = ActivityNewYear::query()->where([
            'status' => 1
        ])
            ->where('start_time', '<', $time)
            ->where('end_time', '>', $time)
            ->orderByDesc('id')
            ->first();
        if(empty($activity_info)){
            return;
        }
        $gift_arr = explode(',', $activity_info['gift_id']) ?? [];
        if (in_array($data['gift_id'], (array)$gift_arr)) {
            // 进行添加逻辑
            // 金额
            // 使用redis =
            $total = $data['total_price'];
            $user_key = 'activity:newYear:richUser:ac_'.$activity_info->id;
            $gift_user_key = 'activity:newYear:charmUser:ac_'.$activity_info->id;
            $room_key = 'activity:newYear:room:ac_'.$activity_info->id;
            app('redis')->zInCrBy($user_key,$total,$data['user_id']);
            app('redis')->zInCrBy($gift_user_key,$total,$data['gift_user']);
            app('redis')->zInCrBy($room_key,$total,$data['room_id']);


//            $key = 'room:cardiac:' . $roomId;
//            $redis->zInCrBy($key, $rough_number, $pit_number);

//            DB::beginTransaction();
//            try {
//                // 存 redis
//                ActivityNewYearUser::query()->where([
//                    'user_id' => $data['user_id'],
//                    'type' => 1
//                ])->update($total);
//                ActivityNewYearUser::query()->where([
//                    'user_id' => $data['gift_user'],
//                    'type' => 2
//                ])->decrement($total);
//                ActivityNewYearUser::query()->where([
//                    'room_id' => $data['room_id'],
//                ])->decrement($total);
//                DB::commit();
//            } catch (\Exception $e) {
//                DB::rollBack();
//                echo "添加失败";
//            }

        }


        // 是否消费礼物
        //获取最近的活动开启

        //strtotime($dateStr);

//        $activity_info = ActivityNewYear::query()->where([
//            'status' => 1
//        ])
//            ->where('start_time', '<', $time)
//            ->where('end_time', '>', $time)
//            ->groupBy('type')
//            ->get();
//        foreach ($activity_info as $val){
//            if($val['type'] == ActivityNewYear::NEWYEAR){
//                //元旦活动
//                $find = $val;
//            }
//            //其他活动
//        }
        // 存 redis 开启  给他倒计时


        // 有礼物消费进来

    }
}
